package com.hunt.controller;

import com.hunt.dao.TransactionCalendarMapper;
import com.hunt.model.app.Stockztpoolem;
import com.hunt.service.StockZTPoolEmService;
import com.hunt.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author tsy
 * @Date 2022/04/06
 * @Description
 */

@Api(value = "涨停板行情接口")

    @Controller
    @RequestMapping(path = "/app/stockztpoolem")
    public class stockztpoolemController extends BaseController{
        @Autowired
        private StockZTPoolEmService stockZTPoolEmService;
        @Autowired
        private TransactionCalendarMapper transactionCalendarMapper;
        @ApiOperation(value = "涨停板行情获取", httpMethod = "GET", produces = "application/json", response = Result.class)
        @ResponseBody
        @RequestMapping(value = "getstockztpoolem", method = RequestMethod.GET)

        public Result getstockztpoolem( @ApiParam(value = " 0是涨停股池,1是昨日涨停股池,2是强势股池,3是炸板股池,4是跌停股池 ")@RequestParam(value = "type", required = true) String type,@ApiParam(value = "排序字段")@RequestParam(value = "orderfield", required = true) String orderfield,@ApiParam(value = "0是正向,1是反向") @RequestParam(value = "sort", required = true) String sort,
                                        @ApiParam(value = "例子20220516")  @RequestParam(value = "datatime",defaultValue ="0") String datestr ) throws ParseException {
            String needdate="";
            //如果是当日的默认值
            if("0".equals(datestr)){
                Date todaydate=new Date();
                SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
                datestr=simpleDateFormat.format(todaydate);
                //如果当日是非开盘日获取上一开盘日数据
                if(!transactionCalendarMapper.findByCalDate(datestr).getIs_open().equals("1")){
                    needdate=transactionCalendarMapper.getbefor(datestr);
                }else {
                    needdate=datestr;
                }
            }else {
                //日期超过什么都不返回
                if(isafter(datestr)){
                    List<Stockztpoolem> szt=new ArrayList<>();
                    return  Result.success(szt);
                }
                //历史日期非开盘日
                if(!transactionCalendarMapper.findByCalDate(datestr).getIs_open().equals("1")){
                    List<Stockztpoolem> szt=new ArrayList<>();
                   return Result.success(szt);
                }else {
                   needdate=datestr;
                }
            }
            if(sort.equals("0")){
                sort="ASC";
            }
            if(sort.equals("1")){

                sort="DESC";
            }
            List<Stockztpoolem> stockztpoolem=null;
            if(orderfield.equals("dalst")){
                stockztpoolem =stockZTPoolEmService.getstockztpoolembydalst(type,sort,needdate);
            }
            else {
                stockztpoolem =stockZTPoolEmService.getstockztpoolem(type,orderfield,sort,needdate);
            }
                return Result.success(stockztpoolem);

        }
    private  boolean  isafter(String date) throws ParseException {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        Date checkdate=  simpleDateFormat.parse(date);
        Date todaydate=new Date();
        if(checkdate.after(todaydate)){
            return  true;
        }
        else {
            return  false;
        }

    }

    }